using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A collection of user attributes.
	/// </summary>
	public sealed class UserAttributeCollection : DatabaseAware, IList, IEnumerable<UserAttribute>
	{
    private List<UserAttribute> attributes;
    private string userName;
    internal UserAttributeCollection(Server server, string userName) : base(server)
    {
      this.userName = userName;
    }

    private void ReadAttributes()
    {
      if (attributes == null)
      {
        attributes = new List<UserAttribute>();
        using (IDataReader reader = Server.ExecuteReader(
          string.Format("SELECT * FROM eAttribute WHERE eUserName = '{0}'", SqlEscape(userName))))
        {
          while (reader.Read()) 
          {
            UserAttribute attribute = new UserAttribute(Server, reader);
            attributes.Add(attribute);
          }  
        }
      }
    }

    #region IList Members

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> is read-only.
    /// </summary>
    public bool IsReadOnly
    {
      get
      {
        return true;
      }
    }

    object IList.this[int index]
    {
      get
      {
        ReadAttributes();
        if ((index >= 0) && (index < Count))
          return attributes[index];
        return null;
      }
      set
      {
        throw new NotSupportedException(readOnlyListMessage);
      }
    }

    /// <summary>
    /// removes the <see cref="T:System.Collections.IList"/>
    /// item at the specified index.
    /// </summary>
    /// <param name="index">The zero-based index of the item to remove.</param>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    /// 	<paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.IList"/>.</exception>
    /// <exception cref="T:System.NotSupportedException">
    /// 	<para>The <see cref="T:System.Collections.IList"/> is read-only.</para>
    /// 	<para>-or-</para>
    /// 	<para>The <see cref="T:System.Collections.IList"/> has a fixed size.</para>
    /// </exception>
    public void RemoveAt(int index)
    {
      ReadAttributes();
      UserAttribute attribute = attributes[index];
      Remove(attribute);
    }

    void IList.Insert(int index, object value)
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    /// <summary>
    /// Removes the specified attribute.
    /// </summary>
    /// <param name="attribute">The attribute to remove.</param>
    public void Remove(UserAttribute attribute)
    {
      ReadAttributes();
      if (attributes.Contains(attribute))
      {
        attribute.Delete();
        attributes.Remove(attribute);
      }
    }

    void IList.Remove(object value)
    {
      Remove((UserAttribute)value);
    }

    bool IList.Contains(object value)
    {
      ReadAttributes();
      return attributes.Contains((UserAttribute)value);
    }

    void IList.Clear()
    {
      throw new NotSupportedException(readOnlyListMessage);
    }

    int IList.IndexOf(object value)
    {
      ReadAttributes();
      return attributes.IndexOf((UserAttribute)value);
    }

    int IList.Add(object value)
    {
      return Add((UserAttribute)value);
    }

    /// <summary>
    /// Adds the specified attribute.
    /// </summary>
    /// <param name="attribute">The attribute.</param>
    /// <returns>The index at which the value has been added.</returns>
    public int Add(UserAttribute attribute)
    {
      ReadAttributes();
      attribute.ApplyChanges();
      attributes.Add(attribute);
      return attributes.IndexOf(attribute);
    }

    /// <summary>
    /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> has a fixed size.
    /// </summary>
    public bool IsFixedSize
    {
      get
      {
        return true;
      }
    }

    #endregion
    #region ICollection Members

    /// <summary>
    /// Gets a value
    /// indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized
    /// (thread-safe).
    /// </summary>
    public bool IsSynchronized
    {
      get
      {
        return false;
      }
    }

    /// <summary>
    /// Gets the number of attributes in the collection.
    /// </summary>
    public int Count
    {
      get
      {
        ReadAttributes();
        return attributes.Count;
      }
    }

    /// <summary>
    /// Copies the elements of
    /// the <see cref="T:System.Collections.ICollection"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
    /// </summary>
    /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
    /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
    /// <exception cref="T:System.ArgumentNullException">
    /// 	<paramref name="array"/> is <see langword="null"/>.</exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">
    /// 	<paramref name="index"/> is less than zero.</exception>
    /// <exception cref="T:System.ArgumentException">
    /// 	<para>
    /// 		<paramref name="array"/> is multidimensional.</para>
    /// 	<para>-or-</para>
    /// 	<para>
    /// 		<paramref name="index"/> is equal to or greater than the length of <paramref name="array"/>.</para>
    /// 	<para>-or-</para>
    /// 	<para>The number of elements in the source <see cref="T:System.Collections.ICollection"/> is greater than the available space from <paramref name="index"/> to the end of the destination <paramref name="array"/>.</para>
    /// </exception>
    /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.ICollection"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
    public void CopyTo(UserAttribute[] array, int index)
    {
      ReadAttributes();
      attributes.CopyTo(array, index);
    }

    void ICollection.CopyTo(Array array, int index)
    {
      CopyTo((UserAttribute[])array, index);
    }

    /// <summary>
    /// Gets an object that
    /// can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
    /// </summary>
    public object SyncRoot
    {
      get
      {
        return null;
      }
    }

    #endregion
    #region IEnumerable Members

    /// <summary>
    /// Returns an enumerator that can iterate through a collection.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.IEnumerator"/>
    /// that can be used to iterate through the collection.
    /// </returns>
    IEnumerator IEnumerable.GetEnumerator()
    {
      ReadAttributes();
      return attributes.GetEnumerator();
    }

    #endregion
    #region IEnumerable<UserAttribute> Members

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
    /// </returns>
    public IEnumerator<UserAttribute> GetEnumerator()
    {
      ReadAttributes();
      return attributes.GetEnumerator();
    }

    #endregion
  }
}
